<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item label="发出部门:" prop="fromOrgId">
        <el-tree-select
          v-model="fromOrgId"
          :clearable="true"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData,
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
              class: 'fromOrgId'
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item label="接收部门:" prop="targetOrgId">
        <el-tree-select
          v-model="targetOrgId"
          :clearable="true"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择',
            loading: $loadingPlugin.getTreeData
          }"
          :tree-params=" {
            'expand-on-click-node': false,
            'highlight-current': true,
            'filterable': true,
            props:{
              value:'orgId',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="sendDt"
          type="date"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="截止日期:">
        <el-date-picker
          v-model="feedBackDt"
          type="date"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="状态:">
        <el-select v-model="query.noticeStatus" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in noticeStatusMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <template v-slot:btns>
        <el-button v-permission="1" type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button v-permission="2" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
    <add ref="add" :notice-degree-map="noticeDegreeMap" />
    <detail ref="detail" :notice-degree-map="noticeDegreeMap" :notice-status-map="noticeStatusMap" />
  </div>
</template>

<script>
  import { queryNoticeList, queryMap } from '@/api'
  import { formatColumnMap, parseTime } from '@/utils'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import moment from 'dayjs'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'
  import Add from './components/Add'
  import Detail from './components/Detail'

  export default {
    name: 'notice',
    components: { Add, Detail },
    data() {
      return {
        treeData: [],
        noticeStatusMap: [],
        noticeDegreeMap: [],
        fromOrgId: '',
        targetOrgId: '',
        sendDt: moment().startOf('day'),
        feedBackDt: '',
        query: {
          noticeStatus: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              prop: 'title',
              label: '通知/公告标题',
              width: 110,
              render: (h, { row }) => <el-link underline={false} onClick={() => this.openDetail(row)}>{row.title}</el-link>
            },
            {
              prop: 'fromOrgName',
              label: '发出部门',
              width: 150
            },
            {
              prop: 'sendName',
              label: '发出人'
            },
            {
              prop: 'targetOrgName',
              label: '接收部门',
              width: 150
            },
            {
              prop: 'receiver',
              label: '接收人'
            },
            {
              prop: 'feedBackName',
              label: '处理人'
            },
            {
              prop: 'noticeDegree',
              label: '紧急程度',
              render: (h, { row }) => <span>{ row.noticeDegree && formatColumnMap(this.noticeDegreeMap, 'value', row.noticeDegree, 'desc') }</span>
            },
            {
              prop: 'sendDt',
              label: '发布时间',
              width: 160,
              render: (h, { row }) => <span>{ row.sendDt && parseTime(row.sendDt, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
            },
            {
              prop: 'noticeStatus',
              label: '状态',
              render: (h, { row }) => <span>{ row.noticeStatus && formatColumnMap(this.noticeStatusMap, 'value', row.noticeStatus, 'desc') }</span>
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    created() {
      this.getTreeData()
      this.queryOptionsMap()
    },
    methods: {
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.orgName,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        if (this.fromOrgId) {
          const org = this.orgList.find(item => item.orgId === this.fromOrgId)
          query.fromOrgId = this.fromOrgId
          query.fromOrgSNNo = org.orgSNNo
          query.fromOrgName = org.orgName
        }
        if (this.targetOrgId) {
          const org = this.orgList.find(item => item.orgId === this.targetOrgId)
          query.targetOrgId = this.targetOrgId
          query.targetOrgSNNo = org.orgSNNo
          query.targetOrgName = org.orgName
        }
        if (this.sendDt) query.sendDt = moment(this.sendDt).format()
        if (this.feedBackDt) query.feedBackDt = moment(this.feedBackDt).format()
        if (query.sendDt > query.feedBackDt) {
          this.$message.error('起始日期不能大于结束日期')
          return
        }
        const res = await queryNoticeList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      async openDetail(row) {
        await this.$refs.detail.open(row)
      },
      async add(row) {
        await this.$refs.add.open(row)
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 查询枚举
      async queryOptionsMap() {
        const [res1, res2] = await Promise.all([mergeAsyncFunc(queryMap, { enumName: 'NoticeStatus' }), mergeAsyncFunc(queryMap, { enumName: 'NoticeDegree' })])
        this.noticeStatusMap = res1.data // 状态
        this.noticeDegreeMap = res2.data // 紧急程度
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
